﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseCoder.Utility
{
    /// <summary>
    /// 主要用来加载各种表单
    /// </summary>
    public class FromFactory
    {
        public FromFactory()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    //文本框
    public static string LoadInputText(string name, string dataType, string defaultValue, params string[] para)
    {
        string inputText = "<input name=\"" + name + "\" type=\"text\" id=\"" + name + "\" width=\"80%\"";
        inputText += FormCheck(dataType, para);
        inputText += "value=\"" + defaultValue + "\"/>";
        return inputText;
    }
    //下拉框
    public static string LoadSelect(string name, string dataType, Dictionary<string, string> options, string selectedValue, params string[] para)
    {
        if (options.Count <= 0) return "未能初始化下拉框" + name + "，因为备选项为空。";

        StringBuilder sb = new StringBuilder();
        sb.Append("<select id=\"" + name + "\" name=\"" + name + "\" ");
        sb.Append(FormCheck(dataType, para));
        sb.Append(">");
        foreach (string key in options.Keys)
        {
            sb.Append("<option value=\"" + options[key] + "\"");
            if (selectedValue == options[key])
            {
                sb.Append(" selected ");
            }
            sb.Append(">" + key + "</option>");
        }
        sb.Append("</option>");
        return sb.ToString();
    }
    //复选框
    public static string LoadCheckBox(string name, string dataType, Dictionary<string, string> options, string[] selectedValue, params string[] para)
    {
        if (options.Count <= 0) return "未能初始化复选框" + name + "，因为备选项为空。";
        return LoadCheckBoxOrRadio("CheckBox", name, dataType, options, selectedValue, para);
        // <input type="checkbox" name="checkbox" id="checkbox" />
    }
    //单选框
    public static string LoadRadio(string name, string dataType, Dictionary<string, string> options, string selectedValue, params string[] para)
    {
        if (options.Count <= 0) return "未能初始化单选框" + name + "，因为备选项为空。";
        return LoadCheckBoxOrRadio("Radio", name, dataType, options, new string[1] { selectedValue }, para);
    }
    private static string LoadCheckBoxOrRadio(string inputType, string name, string dataType, Dictionary<string, string> options, string[] selectedValue, params string[] para)
    {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        foreach (string key in options.Keys)
        {
            sb.Append("<lable><input type=\"" + inputType + "\" name=\"" + name + "\" value=\"" + options[key] + "\"");
            if (i == 0)
            {
                sb.Append(" id=\"" + name + "\"");
                sb.Append(FormCheck(dataType, para));
            }
            else
            {
                sb.Append(" id=\"" + name + i + "\"");
            }
            if (selectedValue.Contains<string>(options[key]))
            {
                sb.Append(" checked ");
            }
            sb.Append(" />" + key + "</lable>");
            i++;
        }

        return sb.ToString();
    }
    //文本域
    public static string LoadTextarea(string name, string dataType, string defaultValue, params string[] para)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<textarea style=\" width:80%; height:120px\" id=\"" + name + "\" name=\"" + name + "\" ");
        sb.Append( FormCheck(dataType, para));
        sb.Append(">" +defaultValue + "</textarea>");
        return sb.ToString();
    }
    //FckEditor
    public static string LoadWebEditor(string name, string dataType, string defaultValue, params string[] para)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<textarea id=\"" + name + "\"  valueFn=\"$.fckValue\"  name=\"" + name + "\" style=\"display:none\" ");
        sb.Append(FormCheck(dataType, para));
        sb.Append(">" + defaultValue + "</textarea>");
        sb.Append("<input type=\"hidden\" id=\"" + name + "Config\" value=\"HtmlEncodeOutput=false\" />");
        sb.Append(" <iframe id=\"" + name + "Frame\" src=\"/fckeditor/editor/fckeditor.html?InstanceName=" + name + "&amp;Toolbar=FontEditor\" width=\"80%\" height=\"200px\" frameborder=\"0\" scrolling=\"no\"></iframe>");

        return sb.ToString();
    }
    private static string FormCheck(string dataType, params string[] para)
    {
        string checkInfo = string.Empty;
        if (!string.IsNullOrEmpty(dataType) && dataType != "不限")
        {
            checkInfo += "dataType=\"" + dataType + "\"";
            if (para.Length > 0)
            {
                checkInfo += "ErrorMsg=\"" + para[0] + "\"";
            }
            if (para.Length > 1)
            {
                checkInfo += " PassMsg=\"" + para[1] + "\"";
            }
        }
        return checkInfo;
    }
    }
}
